

/*
appendix: IV
iv regressions of auction shocks on various measures of asset prices at auctions

output:
- t2b_iv_*.tex: various asset regressions; combined with python

*/


* plotting options
set scheme lean_uncluttered


********************************************************************************
********************************************************************************
n di "intraday regressions"

* merge intraday TAQ shocks
use ../data/auction_notesbonds_govpx_shock.dta, clear
rename auctionDate date
rename cusip auction_cusip
* intraday LQD etf
merge 1:1 date auction_cusip using ../data/input/taq_shock.dta, nogen keep(match)

* for newey-west SEs
sort release_time
gen _T = _n
tsset _T
gen year = year(date)

* rescale shocks to log points, and create label
local taq_tickers LQD HYG GLD SPY IWM MBB VMBS
foreach tic of local taq_tickers {
	replace D_shock`tic' = 100*D_shock`tic'
	label var D_shock`tic' "`tic'"
}


* regressions
eststo clear
foreach tic of local taq_tickers {
	n di "`tic'"
	* regression
	eststo r_`tic': ivreg2 D_shock`tic' (D_shock = D_b2c_all), r bw(9)
	summ year if e(sample)
	estadd local Sample "`r(min)'-`r(max)'"
}
* options for saving tex file
local tex_tab_opts label keep(D_shock) b(2) se(2) ///
	starlevels( * 0.10 ** 0.05 *** 0.01) nonotes nonumbers substitute(\_ _) ///
    stat(N widstat Sample, labels("Obs." "F-stat" "Sample") fmt(0 1 0 ) )

esttab r_* ///
	using ../tables/t2b_iv_asset_price_taq.tex, replace `tex_tab_opts'


********************************************************************************
********************************************************************************
n di "daily regressions"

* merge daily FRED and Datastream shocks
use ../data/auction_notesbonds_govpx_shock.dta, clear
rename auctionDate date
* note: for daily data, only keep later auction if multiple occur in one day
* only a handful of multiple auctions
bys date (close_time): gen _idx_keep = _n==_N
keep if _idx_keep
drop _idx_keep

* daily FRED data
merge 1:1 date using ../data/fred/fred_daily_shocks.dta, ///
	keep(master match) nogen

* daily Datastream data
merge m:1 date using ../data/input/datastream_daily_shocks.dta, ///
	keep(master match) nogen

* fed fund futures
merge 1:1 date using ../data/input/fed_funds_futures_continuous.dta, ///
	keep(master match) nogen
* create fed funds slope
gen ffr_cont_slope_interday = ffr_cont_2m_interday - ffr_cont_1m_interday


* for newey-west SEs
sort release_time
gen _T = _n
tsset _T
gen year = year(date)

* labels
* corp debt
local corpdebt_vars D_MD_AAA D_MD_BAA D_BAML_C
label var D_MD_AAA "Corp. Aaa\( ^\dag\)"
label var D_MD_BAA "Corp. Baa\( ^\dag\)"
label var D_BAML_C "Corp. C\( ^\dag\)"

* equity
local equity_vars Dlog_PSPCOMP Dlog_PFRUSSL2
label var Dlog_PSPCOMP "SP500\( ^\dag\)"
label var Dlog_PFRUSSL2 "Russell 2000\( ^\dag\)"

* inflation/spreads
local infl_comm_spr_vars Dlog_PGSCITOT D_PICAIS10 D_PICAIS2Y ///
	D_LIBOR_OIS_spr D_PUSAUTCD D_PUSBANCD D_VXOCLS
label var Dlog_PGSCITOT "GSCI\( ^\dag\)"
label var D_T10YIE "10Y (TIPS)\( ^\dag\)"
label var D_PICAIS20 "20Y Infl. Swap\( ^\dag\)"
label var D_PICAIS10 "10Y Infl. Swap\( ^\dag\)"
label var D_PICAIS5Y "5Y Infl. Swap\( ^\dag\)"
label var D_PICAIS2Y "2Y Infl. Swap\( ^\dag\)"
*local spread_vars D_MD_AAA_BAA D_LIBOR_OIS_spr D_PUSAUTCD D_PUSBANCD D_VXOCLS 
label var D_LIBOR_OIS_spr "LIBOR-OIS\( ^\dag\)"
label var D_VXOCLS "VIX\( ^\dag\)"
label var D_PUSAUTCD "Auto CDS\( ^\dag\)"
label var D_PUSBANCD "Bank CDS\( ^\dag\)"
label var D_MD_AAA_BAA "Aaa-Baa\( ^\dag\)"


* fed funds
local ffr_vars ffr_cont_1m_interday ffr_cont_slope_interday
label var ffr_cont_1m_interday "1-Month Ahead\( ^\dag\)"
label var ffr_cont_slope_interday "2-Month/1-Month Slope\( ^\dag\)"



* rescale to basis/log points
ds `corpdebt_vars' `equity_vars' `infl_comm_spr_vars'
foreach var of varlist `r(varlist)' {
	* do not rescale CDS
	if "`var'" == "D_PUSAUTCD" | "`var'" == "D_PUSBANCD" {
		continue
	}
	replace `var' = 100*`var'
}

**********************
n di "daily corporate debt"
eststo clear
foreach var of local corpdebt_vars {
	n di "`var'"
	* regression
	eststo r_`var': ivreg2 `var' (D_shock = D_b2c_all), r bw(9)
	summ year if e(sample)
	estadd local Sample "`r(min)'-`r(max)'"
}

esttab r_* ///
	using ../tables/t2b_iv_asset_price_corpdebt.tex, replace `tex_tab_opts'


**********************
n di "daily equities"
eststo clear
foreach var of local equity_vars {
	n di "`var'"
	* regression
	eststo r_`var': ivreg2 `var' (D_shock = D_b2c_all), r bw(9)
	summ year if e(sample)
	estadd local Sample "`r(min)'-`r(max)'"
}

esttab r_* ///
	using ../tables/t2b_iv_asset_price_equity.tex, replace `tex_tab_opts'


**********************
n di "daily commodities/inflation/spreads"
eststo clear
foreach var of local infl_comm_spr_vars {
	n di "`var'"
	* regression
	eststo r_`var': ivreg2 `var' (D_shock = D_b2c_all), r bw(9)
	summ year if e(sample)
	estadd local Sample "`r(min)'-`r(max)'"
}

esttab r_* ///
	using ../tables/t2b_iv_asset_price_infl_comm_spr.tex, replace `tex_tab_opts'


	
**********************
n di "fed fund futures"
eststo clear
foreach var of local ffr_vars {
	n di "`var'"
	* regression
	eststo r_`var': ivreg2 `var' (D_shock = D_b2c_all), r bw(9)
	summ year if e(sample)
	estadd local Sample "`r(min)'-`r(max)'"
}

esttab r_* ///
	using ../tables/t2b_iv_asset_price_ffr.tex, replace `tex_tab_opts'




